package ciyuanwutuobang.Core;

import ciyuanwutuobang.Entity.PlayerInfo;
import ciyuanwutuobang.MainClass;
import org.bukkit.entity.Player;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

/**
 * @author 150149
 */
public class PlayerManager {

    private Map<String, PlayerInfo> playerInfoMap = new ConcurrentHashMap<>();
    private Map<String,Boolean> onlineMap = new ConcurrentHashMap<>();
    private Map<String,ScheduledFuture> tasks = new ConcurrentHashMap<>();

    public void setOnline(Player player,boolean online) {
        onlineMap.put(player.getName(),online);
    }

    public void putTask(Player player,ScheduledFuture task) {
        tasks.put(player.getName(),task);
    }

    public void stopTask(Player player) {
        if (tasks.containsKey(player.getName())) {
            tasks.get(player.getName()).cancel(false);
        }
    }

    public boolean isOnline(Player player) {
        return onlineMap.getOrDefault(player.getName(),false);
    }

    public Map<String, PlayerInfo> getPlayerInfoMap() {
        return playerInfoMap;
    }

    public boolean containsPlayerInfo(Player player) {
        return playerInfoMap.containsKey(player.getName());
    }

    public PlayerInfo getPlayerInfo(Player player) {
        if (containsPlayerInfo(player)) {
            return playerInfoMap.get(player.getName());
        } else {
            PlayerInfo playerInfo = new PlayerInfo(player);
            playerInfo.beenKickCount = MainClass.self.kickCountManager.getKickCount(player);
            playerInfoMap.put(player.getName(),playerInfo);
            return playerInfo;
        }
    }

    public void removePlayer(Player player) {
        playerInfoMap.remove(player.getName());
    }

    public void setPlayerInfo(PlayerInfo playerInfo) {
        playerInfoMap.put(playerInfo.player.getName(),playerInfo);
    }
}
